package com.controle;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.primefaces.event.FlowEvent;

import com.negocio.entidades.CompCurr;
import com.negocio.entidades.Matricula;
import com.negocio.entidades.SituacaoMatricula;
import com.negocio.entidades.Usuario;
import com.negocio.facade.CompCurrRemote;
import com.negocio.facade.MatriculaRemote;

@SessionScoped
@ManagedBean(name = "matriculaMB")
public class MatriculaMB extends AbstractMB implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	private MatriculaRemote matBean;
	@EJB
	private CompCurrRemote compBean;

	private Matricula matricula;
	private List<Matricula> matriculas;
	private Matricula selectedMatricula;
	private List<CompCurr> selectedComponentes;
	private boolean skip;
	private int idSelectedComp;
	private Usuario selectedAluno;
	private boolean salvar = false;

	private static Logger logger = Logger.getLogger(Matricula.class.getName());

	public MatriculaMB() {
		matricula = new Matricula();
		selectedAluno = new Usuario();
		selectedMatricula = new Matricula();
		idSelectedComp = 0;
	}

	// ===============================Gets and Sets
	// ======================================
	public Matricula getMatricula() {
		return matricula;
	}

	public void setMatricula(Matricula matricula) {
		this.matricula = matricula;
	}

	public List<Matricula> getMatriculas() {
		matriculas = matBean.listar();
		return matriculas;
	}

	public void setMatriculas(List<Matricula> matriculas) {
		this.matriculas = matriculas;
	}

	public Matricula getSelectedMatricula() {
		return selectedMatricula;
	}

	public void setSelectedMatricula(Matricula selectedMatricula) {
		this.selectedMatricula = selectedMatricula;
		salvar = true;
	}

	public boolean isSkip() {
		return skip;
	}

	public void setSkip(boolean skip) {
		this.skip = skip;
	}

	public List<CompCurr> getComponentes() {
		return matBean.listarComponentes();
	}

	public List<Usuario> getAlunos() {
		return matBean.listarAlunos();
	}

	public SituacaoMatricula[] getSituacoes() {
		return SituacaoMatricula.values();
	}

	// =======================================================================
	public String onFlowProcess(FlowEvent event) {
		logger.info("Current wizard step:" + event.getOldStep());
		logger.info("Next step:" + event.getNewStep());

		if (skip) {
			skip = false; // reset in case user goes back
			return "confirm";
		} else {
			return event.getNewStep();
		}
	}

	public void doInserir() throws Exception {
		matricula.setAluno(selectedAluno);
		matricula.setCc(selectedMatricula.getCc());
		matricula.setDtMat(new Date());
		matricula.setSituacao(selectedMatricula.getSituacao());
		matBean.salvar(matricula);

	}

	public void doPrepararInserir(ActionEvent actionEvent) throws Exception {
		setSelectedMatricula(new Matricula());
		idSelectedComp = 0;
	}

	public void dlgClose(CloseEvent event) {
		salvar = false;
		idSelectedComp = 0;
	}

	public void doSalvar(ActionEvent actionEvent) throws Exception {
		CompCurr test = selectedMatricula.getCc();
		if (idSelectedComp > 0){
			selectedMatricula.setCc(compBean.consultarPorId(idSelectedComp));
		}
		if (selectedMatricula.getId() == Long.valueOf(0)) {
			try {
				doInserir();
				selectedAluno.getMatriculas().add(selectedMatricula);
				selectedMatricula = new Matricula();
				getAlunos();
				getMatriculas();
				RequestContext.getCurrentInstance().update(
						":listaAlunos :salvarMatricula :listaMatriculasAluno");
				salvar = false;
				idSelectedComp = 0;
			} catch (Exception e) {
				selectedMatricula.setCc(test);
				salvar = true;
				displayErrorMessageToUser("Erro ao salvar Matricula.");
			}

		} else {
			try {

				doAlterar();
				getMatriculas();
				getAlunos();
				getSelectedAluno();
				setSelectedMatricula(new Matricula());
				salvar = false;
				idSelectedComp = 0;
			} catch (Exception e) {
				salvar = true;
				selectedMatricula.setCc(test);
				displayErrorMessageToUser("Erro ao alterar Matricula.");
				RequestContext.getCurrentInstance().update(
						":listaAlunos :salvarMatricula :messages");
			}
		}
	}

	public void doAlterar() throws Exception {
		matBean.salvar(selectedMatricula);
	}

	public void doRemover(Matricula m) {
		try {
			matBean.excluir(m.getId());
			selectedAluno.getMatriculas().remove(m);
		} catch (Exception e) {
			displayErrorMessageToUser("Erro ao excluir Matricula.");
		}
	}

	public List<CompCurr> getSelectedComponentes() {
		return selectedComponentes;
	}

	public void setSelectedComponentes(List<CompCurr> selectedComponentes) {
		this.selectedComponentes = selectedComponentes;
	}

	public Usuario getSelectedAluno() {
		return selectedAluno;
	}

	public void setSelectedAluno(Usuario selectedAluno) {
		this.selectedAluno = selectedAluno;
	}

	public int getIdSelectedComp() {
		return idSelectedComp;
	}

	public void setIdSelectedComp(int idSelectedComp) {
		this.idSelectedComp = idSelectedComp;
	}

	public boolean isSalvar() {
		return salvar;
	}

	public void setSalvar(boolean salvar) {
		this.salvar = salvar;
	}
}
